function Player() {
    Unit.call(this);
	
	this.pos = { x: 0, y: 0 };
    this.size = { x: 8, y: 8 };

	this.speed =  175*2;
	this.color = "#0CED13";
    this.onStairs = true;
	this.attack = false;
	this.defense = false;
	this.die = false;	
	this.immunize = false;
	this.timerImmunize;
	this.moveDirection;
	
	this.valueAttack;
		
	this.update = function(elapsed, keysDown) {
	
		if(!this.isAlive()) {
			return;
		}
		
		var direction = this.moveDirection;
        var move = { x: 0, y: 0 };
		var depl = tileSize/2;
		//if (this.speed * elapsed>depl)
		//	depl = this.speed * elapsed;
		if(touchable || (!(Keys.Left in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && !(Keys.Down in keysDown))) {
			var touch = new Vector2(moveTouchPos.x-moveTouchStartPos.x,moveTouchPos.y-moveTouchStartPos.y);
			var tolerance = 10;
			var depl = tileSize/2;
			if (touch.x>tolerance && touch.y>tolerance) {
				move.x += depl;
				move.y += depl;
				if(direction!=MOVE.LEFT && direction!=MOVE.DOWN)
					direction = MOVE.LEFT;
			}
			else if (touch.x<-tolerance && touch.y>tolerance) {
				move.x -= depl;
				move.y += depl;
				if(direction!=MOVE.RIGHT && direction!=MOVE.DOWN)
					direction = MOVE.RIGHT;
			}
			else if (touch.x>tolerance && touch.y<-tolerance) {
				move.x += depl;
				move.y -= depl;
				if(direction!=MOVE.LEFT && direction!=MOVE.UP)
					direction = MOVE.LEFT;
			}
			else if (touch.x<-tolerance && touch.y<-tolerance) {
				move.x -= depl;
				move.y -= depl;
				if(direction!=MOVE.RIGHT && direction!=MOVE.UP)
					direction = MOVE.RIGHT;
			}
			else if (Math.abs(touch.x)>Math.abs(touch.y)) {
				if(touch.x<0) {
					move.x -= depl;
					direction = MOVE.LEFT;
				}
				else {
					move.x += depl;
					direction = MOVE.RIGHT;
				}
			}
			else if (Math.abs(touch.y)>Math.abs(touch.x)) { 
				if(touch.y<0) {
					move.y -= depl;
					direction = MOVE.UP;
				}
				else {
					move.y += depl;
					direction = MOVE.DOWN;
				}
			}
		} 
		else {
			if ((Keys.Left in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && !(Keys.Down in keysDown)) {
				move.x -= depl;
				direction = MOVE.LEFT;
			}
			else if ((Keys.Right in keysDown) && !(Keys.Left in keysDown) && !(Keys.Up in keysDown) && !(Keys.Down in keysDown)) { 
				move.x += depl;
				direction = MOVE.RIGHT;
			}
			else if ((Keys.Up in keysDown) && !(Keys.Right in keysDown) && !(Keys.Left in keysDown) && !(Keys.Down in keysDown)) {
				move.y -= depl;
				direction = MOVE.UP;
			}
			else if ((Keys.Down in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && !(Keys.Left in keysDown)) {
				move.y += depl;
				direction = MOVE.DOWN;
			}
			else if ((Keys.Left in keysDown) && !(Keys.Right in keysDown) && (Keys.Up in keysDown) && !(Keys.Down in keysDown)) {
				move.x -= depl;
				move.y -= depl;
				if(direction!=MOVE.LEFT && direction!=MOVE.UP)
					direction = MOVE.LEFT;
			}
			else if ((Keys.Right in keysDown) && !(Keys.Left in keysDown) && (Keys.Up in keysDown) && !(Keys.Down in keysDown)) { 
				move.x += depl;
				move.y -= depl;
				if(direction!=MOVE.RIGHT && direction!=MOVE.UP)
					direction = MOVE.RIGHT;
			}
			else if ((Keys.Left in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && (Keys.Down in keysDown)) {
				move.x -= depl;
				move.y += depl;
				if(direction!=MOVE.LEFT && direction!=MOVE.DOWN)
					direction = MOVE.LEFT;
			}
			else if ((Keys.Right in keysDown) && !(Keys.Left in keysDown) && !(Keys.Up in keysDown) && (Keys.Down in keysDown)) { 
				move.x += depl;
				move.y += depl;
				if(direction!=MOVE.RIGHT && direction!=MOVE.DOWN)
					direction = MOVE.RIGHT;
			}	
			
			
		}
		this.moveDirection = direction;
		
		if (Keys.Attack in keysDown || (moveTouchA>0)) { 					
			this.attack = true;
			this.defense = false;
			this.die = false;
				
			this.actionAttack();
		}
		else if (Keys.Defense in keysDown || (moveTouchB>0)) { 				
			this.attack = false;
			this.defense = true;
			this.die = false;
		}
		else {
			this.attack = false;
			this.defense = false;
		}
		
		
		this.pos = levels[currentLevel].moveEntity(this.pos, this.size, this.size, move);
		this.posInMap.x = Math.floor((this.pos.x) / tileSize);
		this.posInMap.y = Math.floor((this.pos.y) / tileSize);
    };
	
	this.draw = function(context, camera) {
		if(!this.isAlive()) {
			return;
		}
		context.font = this.size.x + "px Arial";
		context.fillStyle = this.color;	
		context.fillText("@", this.pos.x - camera.x - (this.size.x/4), this.pos.y - camera.y - this.size.y);
	};
	
	this.actionAttack = function() {
		for (var i = levels[currentLevel].entity.length-1; i >= 0; i--) {
    
			if(levels[currentLevel].entity[i].isAlive()) {
				var ox = levels[currentLevel].entity[i].getPostionInMapX();
				var oy = levels[currentLevel].entity[i].getPostionInMapY();
				var px = this.getPostionInMapX();
				var py = this.getPostionInMapY();
				switch (this.moveDirection) {
					case MOVE.LEFT :
						if((ox == px-1 && oy == py) || (ox == px && oy == py))
							levels[currentLevel].entity[i].setAttack(this.valueAttack);
						break;
					case MOVE.RIGHT :
						if((ox == px+1 && oy == py) || (ox == px && oy == py))
							levels[currentLevel].entity[i].setAttack(this.valueAttack);
						break;
					case MOVE.UP :
						if((ox == px && oy == py-1) || (ox == px && oy == py))
							levels[currentLevel].entity[i].setAttack(this.valueAttack);
						break;
					case MOVE.DOWN :
						if((ox == px && oy == py+1) || (ox == px && oy == py))
							levels[currentLevel].entity[i].setAttack(this.valueAttack);
						break;
				}
			}
		}
	};
	
	this.setValueAttack = function(value) {
		this.valueAttack = value;
	}
	
	this.setAttack = function(attack) {
		if(this.defense == false) {
			this.life -= attack;
			//this.setImmunize();
			if(this.life > 0)
				this.die = false;
			else
				this.die = true;
			
			return true;
		}
		else {
			return false;			
		}
	};
	
	this.setImmunize = function() {
		this.immunize = true;
		//this.timerImmunize = 
		setTimeout(this.immunize = false, 100); 
	}
}